package com.hengjun.baccarat;

import com.hengjun.baccarat.net.UpdateManager;
import com.hengjun.baccarat.net.UrlConn;
import com.hengjun.baccarat.utils.ThreadManager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Register extends Activity {
	private EditText userName;
	private EditText passWord;
	private Button registerButton;
	private String a;
	private Thread thread;
	private EditText beizhu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		ThreadManager.handler = new testHander();
		userName = (EditText) findViewById(R.id.userNameText);
		passWord = (EditText) findViewById(R.id.passWordText);
		registerButton = (Button) findViewById(R.id.registerButton);
		registerButton.setOnClickListener(new RegisterButtonListener());
		beizhu = (EditText) findViewById(R.id.beizhu);
	}
	
	private ProgressDialog dialog;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 0) {
				if (a != null) {
					if (a.equals("0")) {
						Toast.makeText(Register.this, "註冊成功!",
								Toast.LENGTH_SHORT).show();
						Intent intent = new Intent(Register.this, Login.class);
						Register.this.startActivity(intent);
						Register.this.finish();
					} else {
						Toast.makeText(Register.this, "註冊失敗,請聯繫開發商!",
								Toast.LENGTH_SHORT).show();
						thread.interrupt();
					}
				}
			} else if (msg.what == 1) {
				try {
					dialog = ProgressDialog.show(Register.this, "註冊提示",
							"正在註冊,請稍後！");
					dialog.setIcon(R.drawable.new_logo);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if (msg.what == 2) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	};
	private UpdateManager update = new UpdateManager(this);
	@SuppressLint("HandlerLeak")
	private class testHander extends Handler {
		@SuppressLint("HandlerLeak")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			// 更新
			if (msg.what == 1001) {
				update.showNoticeDialog();
			}
		}
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		update.closeUpdate();
		if(exitDialog != null){
			exitDialog.dismiss();
		}
	}

	private Runnable run = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				handler.obtainMessage(1).sendToTarget();
				a = UrlConn.UploadRegister(userName.getText().toString(),
						passWord.getText().toString(), Activity_CheckRegister.tm.getDeviceId(),
						"android", "手機-android", "2014-12-12", beizhu.getText()
								.toString());
				handler.obtainMessage(2).sendToTarget();
				handler.obtainMessage(0).sendToTarget();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				handler.obtainMessage(0).sendToTarget();
			}
		}
	};

	private class RegisterButtonListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			if (!userName.getText().toString().equals("")
					&& !passWord.getText().toString().equals("")
					&& !beizhu.getText().toString().equals("")) {
				
					thread = new Thread(run);
					thread.start();
				
			} else if (userName.getText().toString().equals("")) {
				Toast.makeText(Register.this, "請輸入用戶名!", Toast.LENGTH_SHORT)
						.show();
			} else if (passWord.getText().toString().equals("")) {
				Toast.makeText(Register.this, "請輸入密碼!", Toast.LENGTH_SHORT)
						.show();
			} else if (beizhu.getText().toString().equals("")) {
				Toast.makeText(Register.this, "請輸入备注!", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}
	private Dialog exitDialog = null;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 是否触发按键为back键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitDialog = new Dialog(this, R.style.MyDialogStyle);
			exitDialog.setContentView(R.layout.exit_dialog);
			TextView message = (TextView) exitDialog.findViewById(R.id.textView1);
			message.setText("要退出註冊嗎?");
			Button detemind = (Button) exitDialog.findViewById(R.id.detemind);
			detemind.setText("確認退出");
			detemind.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					exitDialog.dismiss();
					android.os.Process.killProcess(android.os.Process
							.myPid());
					System.exit(0);
				}
			});
			Button cancel = (Button) exitDialog.findViewById(R.id.cancel);
			cancel.setText("返回註冊");
			cancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					exitDialog.dismiss();
				}
			});
			exitDialog.show();
			return true;
		}
		return false;
	}

}
